package com.sbhotel.chotellauncher.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sbhotel.chotellauncher.R;
import com.sbhotel.chotellauncher.constant.Constants;
import com.sbhotel.chotellauncher.entity.AppBean;
import com.sbhotel.chotellauncher.entity.LauncherMainMenuBean;

import java.util.List;

/**
 * @author leihuiming
 * @Description
 */
public class HomeBottomMenuAdapter extends RecyclerView.Adapter<HomeBottomMenuAdapter.ViewHolder> {

    private Context context;
    //默认数据
    List<AppBean> list;
    //网络获取数据
    List<LauncherMainMenuBean.DataBean.ServiceListBean> dataList;

    public HomeBottomMenuAdapter(Context context) {
        this.context = context;
        list = Constants.initHomeBottomMenuData();
        dataList = null;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_bottom_menu, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        dataList = null;
        if(dataList != null && dataList.size() > 0) {
            holder.itemTitle.setText(dataList.get(position).getService_name());
            Glide.with(context).load(dataList.get(position).getService_icon()).into(holder.itemImg);
            holder.itemRL.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnItemClickLitener != null) {
                        mOnItemClickLitener.onBottomMenuItemClick(holder.itemRL, dataList.get(position), position);
                    }
                }
            });
        } else {
            holder.itemTitle.setText(list.get(position).getName());
            holder.itemImg.setImageResource(list.get(position).getIconId());
            holder.itemRL.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnItemClickLitener != null) {
                        mOnItemClickLitener.onBottomMenuItemClick(holder.itemRL, list.get(position), position);
                    }
                }
            });
        }



        /*if(position == 0) {
            badgeView = new QBadgeView(context);
            badgeView.bindTarget(holder.itemParent);
            badgeView.setBadgeBackgroundColor(ContextCompat.getColor(context, R.color.badge_color));
            badgeView.setBadgePadding(6, true);
            badgeView.setGravityOffset(10, true);
            badgeView.setBadgeText("1");
            badgeView.setBadgeTextSize(21, true);
            //badgeView.setBadgeTextColor(ContextCompat.getColor(context, R.color.badge_color));
        }*/
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        public TextView itemTitle;
        public ImageView itemImg;
        public View itemRL;
        //public View itemParent;

        public ViewHolder(View itemView) {
            super(itemView);
            itemTitle = (TextView) itemView.findViewById(R.id.item_app_name);
            itemImg = itemView.findViewById(R.id.item_app_img);
            itemRL = itemView.findViewById(R.id.itemRL);
        }
    }

    public interface OnBottomMenuItemClickLitener {
        void onBottomMenuItemClick(View view, Object data, int position);
    }

    private OnBottomMenuItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnBottomMenuItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public void setDataList(List<LauncherMainMenuBean.DataBean.ServiceListBean> dataList) {
        this.dataList = dataList;
        notifyDataSetChanged();
    }
}
